<template>
	<view class="mdUpload">
		<u-toast ref="uToast" />
		<u-upload ref="uUpload" :key="itemKey" :action="action" :file-list="fileList" show-progress :header="headerData" :disabled="disabled"
			:deletable="!disabled" @on-remove="onRemove" @on-success="onSuccess" @afterRead="onSuccess" @delete="onRemove" :previewFullImage="true">
		</u-upload>
	</view>
</template>

<script>
	import {
		baseUrl
	} from '@/common/config/env.js'
	import request from '@/common/utils/apiUtils/requestApi.js';
	export default {
		props: {
			files: {

			},
      disabled: {
        type: Boolean,
        default: false
      }
		},
		data() {
			return {
				itemKey: 0,
				action: baseUrl + '/sys/filestorage/upload',
				fileList: [],
				url1: 'http://xxxxx:8001/minio/eqmstest',
				url2: 'http://xxxxx:9000/eqmstest',
				// url2: 'http://139.9.160.51:9000/eqmssanhuatest',
			}
		},
		computed: {
			headerData() {
				return {
					Authorization: 'Bearer ' + uni.getStorageSync('mdEqms-token')
				}
			}
		},
		mounted() {
			this.openDialog()
		},
		methods: {

			openDialog() {
				if (Array.isArray(this.files)) {
					this.fileList = this.SYS.deepClone(this.files)
				} else {
					this.fileList = !this.files ? [] : this.SYS.deepClone(JSON.parse(this.files))
				}
				this.fileList.forEach(el => {
					el.url = el.url.replace(this.url1, this.url2)
				})
				console.log(this.fileList)
				this.itemKey++
			},
			replaceUrlChange(fileList) {
				return fileList.map(el => {
					el.url = el.url.replace(this.url2, this.url1)
					return el
				})
			},
			onRemove(obj) {
				console.log(obj)
				this.fileList = this.fileList.filter((v, index) => index != obj.index)
				this.$emit('update:files', JSON.stringify(this.fileList) )
				this.itemKey++
			},
			async onSuccess(data) {
				uni.uploadFile({
					url: baseUrl + '/sys/filestorage/upload',
					header: this.headerData,
					filePath: data.file.url,
					name: 'file',
					success: (su) => {
						let res = JSON.parse(su.data)
						if (res.code !== 0 && res.code !== 200) {
							this.$refs.uToast.show({
								title: res.msg,
								type: 'error',
							})
						} else {
							const newData = res.data.map(v => {
								const urlSuffix = v.split('/')[v.split('/').length - 1]
								const uuidAndSuffix = urlSuffix.split('_')[urlSuffix.split('_')
									.length - 1]
								const name = urlSuffix.replace(`_${uuidAndSuffix}`, '') + '.' +
									uuidAndSuffix
									.split('.')[1]
								return {
									name: name,
									url: v,
									add: true
								}
							})
							const oldData = this.fileList.filter(v => v.add === true)
							const newFileData = JSON.stringify(this.replaceUrlChange([...oldData, ...newData]))
							console.log(newFileData === '' ? [] : JSON.parse(newFileData))
							this.fileList = newFileData === '' ? [] : JSON.parse(newFileData)
							this.itemKey++
							this.$emit('update:files', newFileData)

							console.log(this.fileList)
						}


					}
				});
				return;
				console.log(data)
				let lists = [].concat(data.file)

				console.log(lists)
				let list = new FormData()
				list.append("file", data.file)
				request({
					url: baseUrl + '/sys/filestorage/upload',
					method: 'post',
					data: list
				}).then(res => {

				})


			}
		}
	}
</script>
<style lang="scss">
.mdUpload /deep/ {
  .u-upload__button--disabled {
    display: none !important;
  }

}
</style>
